package com.yykj.hospital.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.common.util.PageRequestUtil;
import com.yykj.common.vo.PageData;
import com.yykj.hospital.server.form.HospitalGuideEditForm;
import com.yykj.hospital.server.form.HospitalGuideListForm;
import com.yykj.hospital.server.model.entity.HospitalGuide;
import com.yykj.hospital.server.model.repository.HospitalGuideRepository;
import com.yykj.hospital.server.service.HospitalGuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HospitalGuideServiceImpl implements HospitalGuideService {
    @Override
    public HospitalGuide editHospitalGuide(HospitalGuideEditForm form) {

        HospitalGuide hospitalGuide;
        if (form.getId() != null && form.getId() > 0) {
            hospitalGuide = guideRepository.findById(form.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        } else {
            hospitalGuide = new HospitalGuide();
        }
        LocalObjectUtil.copyNullValue(form, hospitalGuide);
        hospitalGuide = guideRepository.save(hospitalGuide);

        return hospitalGuide;
    }

    @Override
    public void deleteHospitalGuide(Integer id) {
        guideRepository.deleteById(id);
    }

    @Override
    public PageData<HospitalGuide> listHospitalGuide(HospitalGuideListForm form) {
        if (form.getHospitalId() == null || form.getHospitalId() <= 0) {
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        Pageable pageable = PageRequest.of(PageRequestUtil.getPage(form.getCurrentPage()), PageRequestUtil.getSize(form.getPageSize()), Sort.by(Sort.Order.desc("sorting"),Sort.Order.desc("addTime")));
        Page<HospitalGuide> list = guideRepository.findAll(new Specification<HospitalGuide>() {
            @Override
            public Predicate toPredicate(Root<HospitalGuide> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> predicateList = new ArrayList<>();

                predicateList.add(builder.equal(root.get("hospitalId"), form.getHospitalId()));

                if (!StringUtils.isEmpty(form.getKeyword())) {
                    predicateList.add(builder.like(root.get("title"), "%" + form.getKeyword() + "%"));
                }
                if (form.getStatus() != null) {
                    predicateList.add(builder.equal(root.get("status"), form.getStatus()));
                }

                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        }, pageable);


        return PageData.formatList(list,form.getCurrentPage(),form.getPageSize());
    }

    private HospitalGuideRepository guideRepository;

    @Autowired
    public void setGuideRepository(HospitalGuideRepository guideRepository) {
        this.guideRepository = guideRepository;
    }
}
